package eu.news.servlet.admin.news;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import eu.jk.base.security.AuthFilter;
import eu.news.data.hibernate.dao.HB_NewsDAO;
import eu.news.data.hibernate.pojo.HB_News;
import eu.news.index.NewsIndexDAO;
import eu.news.kits.JsonUtil;
import eu.news.kits.SecurityManager;

/**
 * Servlet implementation class ListNews
 */
public class ListNews extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static final int BLOCK_SIZE=50;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListNews() {
        super();
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String uri = request.getRequestURI();
		String[] fields=uri.split("/");
		int index=0;
		for(;index<fields.length;index++){
			if("list".equals(fields[index])&&index>2)
				break;
		}
		String specialName=null;
		String sortName=null;
		if(fields.length>index+1)
			specialName=URLDecoder.decode(fields[index+1],"UTF-8");
		if(fields.length>index+2)
			sortName=URLDecoder.decode(fields[index+2],"UTF-8");
		request.setAttribute("specialName", specialName);
		request.setAttribute("sortName", sortName);
		request.setAttribute("operator", SecurityManager.getOperator(request));
		request.getRequestDispatcher("/admin/news/list.jsp").forward(request, response);
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uri = request.getRequestURI();
		String[] fields=uri.split("/");
		int index=0;
		for(;index<fields.length;index++){
			if("list".equals(fields[index])&&index>2)
				break;
		}
		String specialName=null;
		String sortName=null;
		String keyword=null;
		if(fields.length>index+1)
			specialName=URLDecoder.decode(fields[index+1],"UTF-8");
		if(fields.length>index+2)
			sortName=URLDecoder.decode(fields[index+2],"UTF-8");
		if(fields.length>index+3)
			keyword=URLDecoder.decode(fields[index+3],"UTF-8");
		String _page=request.getParameter("page");
		int page=0;
		if(AuthFilter.isDigit(_page))
			page=Integer.parseInt(_page);
		/*----------------------*/
		List<HB_News> newses=null;
		if(keyword!=null&&!keyword.equals("_"))
			newses = searchNews(specialName, sortName, keyword);
		else
			newses = getNews(specialName, sortName,page);
		StringBuffer buffer=new StringBuffer("{\"newses\":[");
		for(HB_News news:newses){
			JsonUtil json=new JsonUtil();
			json.put("title", news.getTitle());
			json.put("id", news.getId());
			json.put("sort",news.getSort().getName());
			json.put("operator", news.getOperator().getName());
			buffer.append(json.toString());
			buffer.append(",");
		}
		if(buffer.length()>11)
			buffer.deleteCharAt(buffer.length()-1);
		buffer.append("]}");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(buffer);
	}

	private List<HB_News> searchNews(String specialName,String sortName,String keyword){
		if(specialName.equals("_"))
			specialName=null;
		if(sortName.equals("_"))
			sortName=null;
		HB_NewsDAO nDao=new HB_NewsDAO();
		List<HB_News> news=new ArrayList<HB_News>();
		for(int id:NewsIndexDAO.search(keyword, sortName, specialName, BLOCK_SIZE))
			news.add(nDao.getById(id));
		return news;
	}
	
	private List<HB_News> getNews(String specialName,String sortName,int page){
		HB_NewsDAO dao=new HB_NewsDAO();
		if(specialName!=null&&!specialName.equals("_")){
			if(sortName!=null&&!sortName.equals("_"))
				return dao.getBySpecialSort(specialName, sortName, page*BLOCK_SIZE, BLOCK_SIZE);
			else
				return dao.getBySpecial(specialName,  page*BLOCK_SIZE, BLOCK_SIZE);
		}
		return dao.getAll(page*BLOCK_SIZE, BLOCK_SIZE);
	}
}
